package com.lagou.edu.response;

import com.lagou.edu.utils.HttpProtocolUtil;
import com.lagou.edu.utils.StaticResourceUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author 赵虎
 * @date 2020/11/8 5:52 下午
 */
public class Response {
    private OutputStream outputStream;

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    public void outputHtml(String path) throws IOException {
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            StaticResourceUtil.outputStaticResource(new FileInputStream(file), outputStream);
        } else {
            String httpHeader404 = HttpProtocolUtil.getHttpHeader404();
            output(httpHeader404);
        }
    }
}
